package services

import (
	"errors"
	"lingc/models"
	"time"

	"gorm.io/gorm"
)

// MemberLevelService 提供账号登录校验的服务
type MemberLevelService struct {
	DB *gorm.DB
}

// NewMemberLevelService 创建一个新的 MemberLevelService 实例
func NewMemberLevelService(db *gorm.DB) *MemberLevelService {
	return &MemberLevelService{DB: db}
}

//表单业务

/**
 * @Description: 分页获取会员等级列表
 * @param where
 * @param page
 * @param pageSize
 * @return []*models.MemberLevel
 * @return int
 * @return int64
 * @return error
 */
func (s *MemberLevelService) PageMemberLevelList(where string, page, pageSize int) ([]*models.MemberLevelList, int, int64, error) {
	var data []*models.MemberLevel
	var total int64

	// 计算偏移量
	offset := (page - 1) * pageSize

	// 获取总记录数
	if err := s.DB.Model(&models.MemberLevel{}).Where(where).Count(&total).Error; err != nil {
		return nil, 1, 0, err
	}

	// 获取分页数据
	if err := s.DB.Where(where).Offset(offset).Limit(pageSize).Order("id DESC").Find(&data).Error; err != nil {
		return nil, 1, 0, err
	}
	var list []*models.MemberLevelList
	list = []*models.MemberLevelList{}
	for _, data := range data {
		list = append(list, &models.MemberLevelList{
			ID:        data.ID,
			LevelID:   data.LevelID,
			LevelName: data.LevelName,
			Money:     data.Money,
			Src:       data.Src,
			Duration:  data.Duration,
			AppID:     data.AppID,
			CreatedAt: time.Unix(data.CreatedAt, 0).Format("2006-01-02 15:04:05"), // 格式化时间戳
			UpdatedAt: time.Unix(data.UpdatedAt, 0).Format("2006-01-02 15:04:05"), // 格式化时间戳
		})
	}

	return list, page, total, nil
}

/**
 * @Description: 创建新会员等级
 * @param username
 * @param password
 * @param realname
 * @param avatar
 * @param status
 * @return *models.MemberLevelCreate
 * @return error
 */
func (s *MemberLevelService) CreateMemberLevel(data *models.MemberLevelCreate) (*models.MemberLevelCreate, error) {
	// 检查会员等级是否已存在
	var existingData models.MemberLevel
	if err := s.DB.Where("title = ?", data.LevelID).First(&existingData).Error; err == nil {
		return nil, errors.New("数据已存在")
	}
	addData := &models.MemberLevel{
		LevelID:   data.LevelID,
		LevelName: data.LevelName,
		Money:     data.Money,
		Src:       data.Src,
		Duration:  data.Duration,
		CreatedAt: time.Now().Unix(),
		UpdatedAt: time.Now().Unix(),
	}
	// 保存新会员等级到数据库
	if err := s.DB.Create(addData).Error; err != nil {
		return nil, err
	}
	return data, nil
}

// GetMemberLevel 获取会员等级信息
func (s *MemberLevelService) GetMemberLevel(where string) (*models.MemberLevel, error) {
	var data models.MemberLevel
	if err := s.DB.Where(where).First(&data).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, errors.New("数据不存在")
		}
		return nil, err
	}
	// log.Printf("data_info:%v", data)
	return &data, nil
}

/**
 * @Description: 更新会员等级信息
 * @param data
 * @return error
 */
func (s *MemberLevelService) UpdateMemberLevel(group *models.MemberLevelUpdate) (*models.MemberLevelUpdate, error) {
	// 检查会员等级是否已存在
	var existingData models.MemberLevel
	if err := s.DB.Where("id = ?", group.ID).First(&existingData).Error; err != nil {
		return nil, errors.New("数据不存在")
	}
	existingData.LevelID = group.LevelID
	existingData.LevelName = group.LevelName
	existingData.Money = group.Money
	existingData.Src = group.Src
	existingData.Duration = group.Duration
	existingData.UpdatedAt = time.Now().Unix()
	return group, s.DB.Save(existingData).Error
}

/**
 * @Description: 删除会员等级
 * @param id
 * @return error
 */
func (s *MemberLevelService) DeleteMemberLevel(id uint) error {
	var group models.MemberLevel
	if err := s.DB.Where("id = ?", id).First(&group).Error; err != nil {
		return errors.New("数据不存在")
	}
	return s.DB.Delete(&group).Error
}

/**
 * @Description: 批量删除会员等级
 * @param id
 * @return error
 */
func (s *MemberLevelService) DeleteMemberLevels(ids []string) error {
	for _, id := range ids {
		var group models.MemberLevel
		if err := s.DB.Where("id = ?", id).First(&group).Error; err != nil {
			return errors.New("数据不存在")
		}
		err := s.DB.Delete(&group).Error
		if err != nil {
			return errors.New("删除失败")
		}
	}
	return nil
}
